﻿using System;
using System.Windows.Forms;
using Strom.Client.Network;
using Strom.Core.Network;
using Strom.Core.Network.Packets;

namespace Strom.Client.GUI
{
    public partial class FormRegister : Form
    {
        public FormRegister()
        {
            InitializeComponent();
            ClientManager.GUI.Register = this;
        }

        private void txt_pass_TextChanged(object sender, EventArgs e)
        {
            TryToEnableButton();
        }

        private void txt_login_TextChanged(object sender, EventArgs e)
        {
            TryToEnableButton();
        }

        private void txt_pass_confirm_TextChanged(object sender, EventArgs e)
        {
            TryToEnableButton();
        }

        void TryToEnableButton()
        {
            if (txt_login.Text.Length > 3 && txt_pass.Text.Length > 3 && txt_pass.Text == txt_pass_confirm.Text)
                btn_register.Enabled = true;
            else
                btn_register.Enabled = false;
        }

        private void btn_register_Click(object sender, EventArgs e)
        {
            BaseClient client = new BaseClient();
            Network.Parser.RegisterNewClientOnParser(client);

            try
            {
                client.Connect(RemoteServerConfig.ServerIP, RemoteServerConfig.ServerPort);
            }
            catch
            {
                MessageBox.Show("Impossible de se connecter au serveur.", "Strøm", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            if (client.IsConnected)
                new LoginRequestPacket().Pack(client, (byte)0, txt_login.Text, txt_pass.Text);
        }

        protected override void OnClosed(EventArgs e)
        {
            ClientManager.GUI.Register = null;
            base.OnClosed(e);
        }

        private void FormRegister_Load(object sender, EventArgs e)
        {

        }
    }
}
